package com.mrd.gtimp.business.action;


import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.business.entity.FundFlow;
import com.mrd.gtimp.business.service.FundFlowBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/fund")
public class FundFlowAction {
    @Resource
    private FundFlowBiz biz;

    @RequestMapping("/flow-main")
    public String main() {
        HttpUtils.getRequest().setAttribute("balance", biz.findCurrentBalance(LoginUtil.getLoginUser().getCompany().getId()));
        return "/gtimp/fundFlow/customer_main";
    }

    @RequestMapping("/flow-supMain")
    public String supMain() {
        HttpUtils.getRequest().setAttribute("balance", biz.findCurrentBalance(LoginUtil.getLoginUser().getCompany().getId()));
        return "/gtimp/fundFlow/sup_main";
    }

    @RequestMapping("/flow-view")
    public String view() {
        HttpUtils.getRequest().setAttribute("id", HttpUtils.getRequest().getParameter("id"));
        return "/gtimp/fundFlow/customer_edit";
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/flow-find")
    @ResponseBody
    @Json({@JField("id"), @JField("billNo"), @JField("amount"), @JField("financialType"), @JField("ietype"), @JField("balance")})
    public FundFlow find(FundFlow fundFlow) {
        if (null != fundFlow && StringUtils.isNotEmpty(fundFlow.getId())) {
            if (StringUtils.isNotEmpty(fundFlow.getId())) {
                try {
                    fundFlow = biz.findEntityById(fundFlow.getId());
                    return fundFlow;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    @RequestMapping("/flow-findSupList")
    @ResponseBody
    @Json({@JField("id"), @JField("billNo"), @JField("owner.name"), @JField("amount"), @JField("financialType"),
            @JField("ietype"), @JField("balance"), @JField("regDate")})
    public Page<FundFlow> findSupList(Jqgrid jqgrid) {
        String comId = LoginUtil.getLoginUser().getCompany().getId();// get from LoginUtil.getLoginUser()
        try {
            SearchRule rule = new SearchRule("owner.id", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
            Page<FundFlow> page = biz.findByJqgrid(jqgrid);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/flow-findList")
    @ResponseBody
    @Json({@JField("id"), @JField("billNo"), @JField("owner.name"), @JField("amount"), @JField("financialType"),
            @JField("ietype"), @JField("balance"), @JField("regDate")})
    public Page<FundFlow> findList(Jqgrid jqgrid) {
        String comId = LoginUtil.getLoginUser().getCompany().getId();// get from LoginUtil.getLoginUser()
        try {
            SearchRule rule = new SearchRule("owner.id", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
            Page<FundFlow> page = biz.findByJqgrid(jqgrid);
            if (page.getSize() > 0) {
                return page;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public FundFlowBiz getBiz() {
        return biz;
    }

    public void setBiz(FundFlowBiz biz) {
        this.biz = biz;
    }
}
